<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Home\DouyinListController;
use Illuminate\Http\Request;
use App\Models\DyModels\ReceiveMsg;
use DB;

class AuthController extends Controller
{
    /**
     *    订阅事件消息
     */

    public function __construct(Request $request)
    {
        $this->params = $request->all();
    }

    //接收订阅事件消息
    function Webhooks()
    {
        $DouyinListController = new DouyinListController();
        $body = file_get_contents("php://input");//获取提交过来的数据
        $data = json_decode($body, true); //对获取的数据进行解析

        if (isset($data['event']) && $data['event'] == 'verify_webhook') {    //抖音验证网址回传
            echo json_encode($data['content']);
            exit;
        } elseif (isset($data['event']) && $data['event'] == 'create_video') {

        } elseif (isset($data['event']) && $data['event'] == 'authorize') {

        } elseif (isset($data['event']) && $data['event'] == 'unauthorize') {

        } elseif (isset($data['event']) && $data['event'] == 'receive_msg') {    //接收私信事件
          //  print_r();exit();
            if ($data['content']['message_type'] = "text" && !empty($data['to_user_id']) && !empty($data['from_user_id'])) {//如果私信消息是文本类型的话
            //    print_r(2222);exit();
                //查询对应的openid设置的对应回复关键词和回复词
                $resa = $this->checktext($data['to_user_id']);
           //     print_r($resa);
                //判断是否存在对应的词 如果有就开启私聊回复
                if(is_array($resa)){
                    foreach ($resa as $k =>$v){
                        if (strpos($data['content']['text'],$v['search_word'])!==false){//如果发送的私信包含
                            $this->saveMsgInfo($data);
                            //确定是
                       //     print_r($resa);exit();
                            $DouyinListController->sendToFans($data['to_user_id'],$v['reply_word'],$data['from_user_id'],$data['content']['message_type']);
                        }
                    }
                } else {//直接存数据库
                    //入库
                    $this->saveMsgInfo($data);

                }
            }
        } elseif (isset($data['event']) && $data['event'] == 'enter_im') {

        } elseif (isset($data['event']) && $data['event'] == 'dial_phone') {

        } elseif (isset($data['event']) && $data['event'] == 'website_contact') {

        } elseif (isset($data['event']) && $data['event'] == 'personaltabcontact') {

        } elseif (isset($data['event']) && $data['event'] == 'item_comment_reply') {    //接收评论回复事件
            if ($data['content']['message_type'] = "text") {//如果评论消息是文本类型的话
                $resa = $this->checkcommenttext($data['to_user_id'], $data['content']['text']);
                if (!empty($resa)) {//如果文本信息符合关键词条件 需要判断
                    //入库然后回复评论 --
                    $this->savecommentInfo($data);
                    $date = array(
                        'popenid' => $data['to_user_id'],
                        'comment_id' => $data['to_user_id']['comment_user_id'],
                        'content' => $resa,
                        'item_id' => $data['to_user_id']['comment_id '],
                    );
                    $DouyinListController = new DouyinListController;
                    $DouyinListController->sendToCommnet($date);
                } else {
                    //入库
                    $this->saveMsgInfo($data);
                }
            }
        } else {
            exit("数据错误");
        }
    }


    //查询对应的私聊信息内容有没有符合的对应数据
    function checktext($openid)
    {
        $sql = "select reply_word,search_word from pt_keyword_automatic where spare=1 and reply_type=1 and status=1 and open_id='$openid' ";
        $res = DB::select($sql);
       return isset($res) ? $res : '';

    }

    //查询对应的私聊信息内容有没有符合的对应数据
    function checkcommenttext($openid, $content)
    {
        $sql = "select reply_word from pt_keyword_automatic where spare=1 and reply_type=2 and status=1 and open_id='$openid' and search_word like '%$content%'";
        $res = DB::select($sql);
        return isset($res[0]['reply_word']) ? $res[0]['reply_word'] : '';
    }
    //订阅的评论入库方法
    function savecommentInfo($info)
    {
        $data = array(
            'event' => isset($info['event']) ? $info['event'] : '',
            'from_user_id' => isset($info['from_user_id']) ? $info['from_user_id'] : '',
            'to_user_id' => isset($info['to_user_id']) ? $info['to_user_id'] : '',
            'client_key' => isset($info['client_key']) ? $info['client_key'] : '',
            'statues' => 0,
            'create_time' => date('Y-m-d H:i:s', time()),
            'update_time' => date('Y-m-d H:i:s', time()),
            'comment_id' => isset($info['content']['comment_id']) ? $info['content']['comment_id'] : '',
            'comment_user_id' => isset($info['content']['comment_user_id']) ? $info['content']['comment_user_id'] : '',
            'content' => isset($info['content']['content']) ? $info['content']['content'] : '',
            'digg_count' => isset($info['content']['digg_count']) ? $info['content']['digg_count'] : '',
            'reply_comment_total' => isset($info['content']['reply_comment_total']) ? $info['content']['reply_comment_total'] : '',

        );
        DB::table('pt_itemcomment')->insert($data);
    }

    //订阅的私信入库方法
    function saveMsgInfo($info)
    {
        $data = array(
            'message_type' => isset($info['content']['message_type']) ? $info['content']['message_type'] : '',
            'text' => isset($info['content']['text']) ? $info['content']['text'] : '',
            'from_user_id' => isset($info['from_user_id']) ? $info['from_user_id'] : '',
            'to_user_id' => isset($info['to_user_id']) ? $info['to_user_id'] : '',
            'client_key' => isset($info['client_key']) ? $info['client_key'] : '',
            'statues' => 0,
            'creatime' => date('Y-m-d H:i:s', time()),
            'updatatime' => date('Y-m-d H:i:s', time()),
            'resource_type' => isset($info['content']['resource_type']) ? $info['content']['resource_type'] : '',
            'resource_url' => isset($info['content']['resource_url']) ? $info['content']['resource_url'] : '',
            'link_url' => isset($info['content']['link_url']) ? $info['content']['link_url'] : '',
            'title' => isset($info['content']['title']) ? $info['content']['title'] : '',
            'description' => isset($info['content']['description']) ? $info['content']['description'] : '',
            'action_name' => isset($info['content']['actions']['name']) ? $info['content']['actions']['name'] : '',
            'action_value' => isset($info['content']['actions']['action_value']) ? $info['content']['actions']['action_value'] : '',
            'action_type' => isset($info['content']['actions']['action_type']) ? $info['content']['actions']['action_type'] : '',
        );
        //    $oReceiveMsg=new ReceiveMsg();
        DB::table('pt_dy_receive_msg')->insert($data);
    }

    //查询access_token的方法
    public function getaccesstoken($open_id)
    {
        $sql = " select access_token from pt_dy_access_token where open_id='$open_id' ";
        $token = DB::select($sql);
        return $token;
    }

    //根据openid以及备用信息spare和回复类型reply_type查询出对应的关键字和回复词
    public function getAllInfo($openid, $reply_type)
    {
        $sql = "select search_word,reply_word,open_id from pt_keyword_automatic where open_id='$openid' and spare=1 and reply_type=$reply_type";
        $openid = DB::select($sql);
        return $openid;
    }


}